package myapp.commands 
{
	import myapp.Model;
	import myapp.data.FlickrImage;

	import com.bumpslide.command.CommandEvent;
	import com.bumpslide.command.ServiceCall;
	import com.bumpslide.net.XMLRequest;

	import flash.net.URLRequest;

	/**
	 * 
	 * @author David Knape
	 */
	public class LoadFlickrSet extends ServiceCall 
	{	
		
		private const FLICKR_SET_FEED_URL:String = "http://api.flickr.com/services/feeds/photoset.gne?&lang=en-us&format=rss_200";
		
		private var model:Model = Model.getInstance();
		
		override public function execute(event:CommandEvent):void {
			callback = event;
			
			//debugEnabled = true;
			
			var url:String = FLICKR_SET_FEED_URL 
				+ '&nsid='+model.config.flickrNsid 
				+ '&set='+model.config.flickrSet;
			
			var xmlReq:XMLRequest = new XMLRequest( new URLRequest( url ), this );
			xmlReq.debugEnabled = true;
			rpc = xmlReq;
			rpc.load();
		}

		
		override public function result(data:Object):void
		{
			var xml:XML = data as XML;
			
			var images:Array = new Array();
			for each (var item:XML in xml.channel.item) {
				images.push(FlickrImage.createFromRSS(item));         	
			}          
			
			//trace('Flickr XML loaded: ' + images);
			var link:String = xml.channel.link;
			var title:String = xml.channel.title.toString().replace('Content from ', '');
			model.titleHtml = '<a href="'+link+'">'+title + '</a>';
			model.images = images;
			super.result( images );
		}
	}
}
